package io.plus.server.store;

import io.plus.common.Constants;
import io.plus.common.packet.Group;
import io.plus.common.packet.User;
import io.plus.server.UserConfiguration;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import tool.plus.$;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Data
@Slf4j
public class StoreImpl implements Store, Constants {

    public final Map<String, User> tokenToUserMap = new HashMap<>();
    protected UserConfiguration userConfiguration;


    public StoreImpl(UserConfiguration userConfiguration) {

        this.userConfiguration = userConfiguration;
    }

    @Override
    public User getUser(String userId) {
        if (userId == null) {
            return null;
        }
        User user = null;
        return user;
    }

    @Override
    public boolean isOnline(String userId) {
        //需要从当前的所有的用户信息找出是否存在
        return false;
    }
    //

    /**
     * 获取群组所有成员信息
     */
    @Override
    public Group getGroup(String groupId) {
        if (groupId == null) {
            return null;
        }
        Group group = new Group();
        List<String> userIdList = this.getGroupUserIdList(groupId);
        if ($.isNotNullAndHasElement(userIdList)) {
            return group;
        }
        List<User> userList = new ArrayList<User>();
        for (String userId : userIdList) {
            User user = getUser(userId);
            if (user != null) {
                userList.add(user);
            }
        }
        group.setUserList(userList);
        return group;
    }

    @Override
    public List<String> getGroupUserIdList(String groupId) {
        List<String> userIdList = new ArrayList<String>();
        return userIdList;
    }

    //

    /**
     * 获取群组所有成员信息（在线+离线)
     */
    @Override
    public List<Group> getGroupList(String userId) {
        if (userId == null) {
            return null;
        }
        List<String> groupIdList = new ArrayList<String>();
        if ($.isNotNullAndHasElement(groupIdList)) {
            return null;
        }
        List<Group> groupList = new ArrayList<Group>();
        for (String groupId : groupIdList) {
            Group group = getGroup(groupId);
            if (group != null) {
                groupList.add(group);
            }
        }
        return groupList;
    }

    @Override
    public List<String> getGroupIdList(String userId) {
        List<String> groupIdList = new ArrayList<String>();
        return groupIdList;
    }


}
